package com.fwtai.api;

import com.fwtai.bean.PageFormData;
import com.fwtai.datasource.DaoHandle;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;

/**
 * 微信支付订单访问数据库
 * @作者 李宇鹏
 * @版本 v1.0
 * @创建时间 2023-04-11 13:44:32
 * @QQ号码 444141300
 * @Email service@yinlz.com
 * @官网 <url>http://www.fwtai.com</url>
*/
@Repository
public class ApiGoodsOrderDao{

  private final DaoHandle dao;

  public ApiGoodsOrderDao(final DaoHandle dao){
    this.dao = dao;
  }

  public int add(final PageFormData formData){
    return dao.execute("api_goods_order.add",formData);
  }

  public int edit(final PageFormData formData){
    return dao.execute("api_goods_order.edit",formData);
  }

  public Long queryExistById(final long kid){
    return dao.queryForLong("api_goods_order.queryExistById",kid);
  }

  public HashMap<String,Object> queryById(final long kid){
    return dao.queryForHashMap("api_goods_order.queryById",kid);
  }

  public HashMap<String,Object> queryIntegralOrderInfo(final long kid){
    return dao.queryForHashMap("api_goods_order.queryIntegralOrderInfo",kid);
  }

  public int delById(final long kid){
    return dao.execute("api_goods_order.delById",kid);
  }

  public int delByKeys(final long[] list){
    return dao.execute("api_goods_order.delByKeys",list);
  }

  public List<HashMap<String,Object>> getListData(final PageFormData formData){
    return dao.queryForListHashMap("api_goods_order.listData",formData);
  }

  public List<HashMap<String,Object>> listIntegralOrder(final PageFormData formData){
    return dao.queryForListHashMap("api_goods_order.listIntegralOrder",formData);
  }

  public Integer getListTotal(final PageFormData formData){
    return dao.queryForInteger("api_goods_order.listTotal",formData);
  }

  public List<HashMap<String,Object>> getOrderDetails(final long kid){
    return dao.queryForListHashMap("api_goods_order_detail.getOrderDetails",kid);
  }

  public List<HashMap<String,Object>> getIntegralDetails(final long kid){
    return dao.queryForListHashMap("api_goods_order.getIntegralDetails",kid);
  }

  public List<HashMap<String,Object>> getAllIntegral(final long loginId){
    return dao.queryForListHashMap("api_goods_order.getAllIntegral",loginId);
  }

  public List<HashMap<String,Object>> getAllOrder(final long loginId){
    return dao.queryForListHashMap("api_goods_order.getAllOrder",loginId);
  }

  public List<HashMap<String,Object>> getIntegralList(final PageFormData formData){
    return dao.queryForListHashMap("api_goods_order.getIntegralList",formData);
  }

  public List<HashMap<String,Object>> getIntegralSalesOrder(final PageFormData formData){
    return dao.queryForListHashMap("api_goods_order.getIntegralSalesOrder",formData);
  }

  public int addOrderNotes(final PageFormData formData,final Integer type){
    switch (type){
      case 1:
        return dao.execute("api_goods_order.addOrderNotes",formData);
      case 2:
        return dao.execute("api_goods_order.addIntegralNotes",formData);
      default:
        return 0;
    }
  }

  public List<HashMap<String,Object>> queryOrderList(final PageFormData formData){
    return dao.queryForListHashMap("api_goods_order.queryOrderList",formData);
  }

  public Integer queryIncomeOrderType(final long out_trade_no){
    return dao.queryForInteger("api_goods_order.queryIncomeOrderType",out_trade_no);
  }

  public HashMap<String,Object> queryIncomeOrder(final long out_trade_no,final Integer payType){
    switch (payType){
      case 1:
        return dao.queryForHashMap("api_goods_order.queryIncomeOrder1",out_trade_no);
      case 2:
        return dao.queryForHashMap("api_goods_order.queryIncomeOrder2",out_trade_no);
      case 3:
        return dao.queryForHashMap("api_goods_order.queryMallOrder",out_trade_no);
      default:
        return null;
    }
  }

  public List<HashMap<String,Object>> getIncomeOrderDetails(final long kid,final Integer payType){
    switch (payType){
      case 1:
        return dao.queryForListHashMap("api_goods_order_detail.getOrderDetails",kid);
      case 2:
        return dao.queryForListHashMap("api_goods_order.getIntegralDetails",kid);
      case 3:
        return dao.queryForListHashMap("api_goods_order.getMallDetails",kid);
      default:
        return null;
    }
  }

  public List<HashMap<String,Object>> getAllOrderList(final PageFormData formData){
    return dao.queryForListHashMap("api_goods_order.getAllOrderList",formData);
  }

  public List<HashMap<String,Object>> getAccountIn(final long outTradeNo){
    return dao.queryForListHashMap("api_goods_order.getAccountIn",outTradeNo);
  }

  public int editWechatStatus(final HashMap<String,Long> params){
    return dao.execute("api_goods_order.editWechatStatus",params);
  }

  public int editDeliveryWechat(final long out_trade_no){
    return dao.execute("api_goods_order.editDeliveryWechat",out_trade_no);
  }

  public int editDeliveryIntegral(final long out_trade_no){
    return dao.execute("api_goods_order.editDeliveryIntegral",out_trade_no);
  }

  public int editAffirmDelivery(final long out_trade_no){
    return dao.execute("api_goods_order.editAffirmDelivery",out_trade_no);
  }

  @Transactional
  public int editWechatStatus(final HashMap<String,Long> params,final List<HashMap<String,Object>> listAccount){
    final int rows = dao.execute("api_goods_order.editWechatStatus",params);
    if(rows > 0){
      dao.execute("api_goods_order.editAccountStatus",params);
      dao.execute("api_goods_order.update_user_account",listAccount);
    }
    return rows;
  }

  @Transactional
  public int editWechatDelivery(final long out_trade_no,final HashMap<String,Long> params,final List<HashMap<String,Object>> listAccount){
    final int rows = dao.execute("api_goods_order.editDeliveryWechat",out_trade_no);
    if(rows > 0){
      dao.execute("api_goods_order.editAccountStatus",params);
      dao.execute("api_goods_order.update_user_account",listAccount);
    }
    return rows;
  }

  @Transactional
  public int editIntegralDelivery(final long out_trade_no,final HashMap<String,Long> params,final List<HashMap<String,Object>> listAccount){
    final int rows = dao.execute("api_goods_order.editDeliveryIntegral",out_trade_no);
    if(rows > 0){
      dao.execute("api_goods_order.editAccountStatus",params);
      dao.execute("api_goods_order.update_user_account",listAccount);
    }
    return rows;
  }

  public int editIntegralStatus(final HashMap<String,Long> params){
    return dao.execute("api_goods_order.editIntegralStatus",params);
  }

  @Transactional
  public int editIntegralStatus(final HashMap<String,Long> params,final List<HashMap<String,Object>> listAccount){
    final int rows = dao.execute("api_goods_order.editIntegralStatus",params);
    if(rows > 0){
      dao.execute("api_goods_order.editAccountStatus",params);
      dao.execute("api_goods_order.update_user_account",listAccount);
    }
    return rows;
  }
}